<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Introduction</title>
<link rel="stylesheet" href="udtdoc.css" type="text/css" />
</head>

<body>
<div class="ref_head">&nbsp;UDT Tutorial</div>

<h3><font color="#000080">Transfering Data using UDT</font></h3>
<p>This section describes using UDT to transfer data in streaming mode. This is exactly the same as using traditional BSD socket.</p>

<p>In streaming mode, neither a send or a recv call can guarantee that all data are sent or received in one call, because there is no boundary information in the data stream. Application 
should use loops for both sending and receiving.</p>

<p><strong>Example: send a data block (buf, size) using UDT.</strong></p>
<div class="code">
      int ssize = 0;<br>
      int ss;<br>
      while (ssize < size)<br>
      {<br>
      &nbsp;&nbsp;if (UDT::ERROR == (ss = UDT::send(usock, buf + ssize, size - ssize, 0)))<br>
      &nbsp;&nbsp;{<br>
      &nbsp;&nbsp;&nbsp;&nbsp;cout << "send:" << UDT::getlasterror().getErrorMessage() << endl;<br>
      &nbsp;&nbsp;&nbsp;&nbsp;break;<br>
      &nbsp;&nbsp;}<br>
<br>
      &nbsp;&nbsp;ssize += ss;<br>
      }
</div>

<p>Similarily, to receive data stream, the following example code can be used.</p>
<p><strong>Example: receive &quot;size&quot; of data into buffer &quot;buf&quot; </strong></p>
<div class="code">
      int rsize = 0;<br>
      int rs;<br>
      while (rsize < size)<br>
      {<br>
      &nbsp;&nbsp;if (UDT::ERROR == (rs = UDT::recv(usock, buf + rsize, size - rsize, 0)))<br>
      &nbsp;&nbsp;<br>
      &nbsp;&nbsp;&nbsp;&nbsp;cout << "recv:" << UDT::getlasterror().getErrorMessage() << endl;<br>
      &nbsp;&nbsp;&nbsp;&nbsp;break;<br>
      &nbsp;&nbsp;}<br>
<br>
       &nbsp;&nbsp;rsize += rs;<br>
      }
</div>

<h5>Blocking vs. Non-blocking</h5>
<p>UDT supports both blocking and non-blocking mode. The above example demonstrated the blocking mode. In non-blocking mode, UDT::send and UDT::recv will return immediately if there is 
no buffer available. Usually, non-blocking calls are used together with accept.</p>
<p>UDT also supports timed blocking IO with UDT_SNDTIMEO and UDT_RCVTIMEO. This is in the middle between complete blocking and complete non-blocking calls. Timed IO will block the 
sending or receiving call for a limited period. This is sometimes useful if the application does not know if and when the peer side will send a message.</p>

<p>&nbsp;</p>
</body>
</html>
